﻿#include "regionscanform.h"
#include "ui_regionscanform.h"

RegionScanForm::RegionScanForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegionScanForm)
{
    ui->setupUi(this);
    this->setObjectName("WidgetObjectRegionScanForm-ChinascanNormal-RegionScan-MoonScan");
    this->setWindowTitle(tr("RegionScanForm"));
    ui->doubleSpinBox_Cycle->setValue(LoopTime);
    ui->doubleSpinBox_ScanSpeed->setValue(TaskManager::getInstance()->currentInstrument()->mdMirrorScanSpeed);
    ui->doubleSpinBox_EWMax->setValue(TaskManager::getInstance()->currentInstrument()->mdChinaNormalEWMax);
    ui->doubleSpinBox_EWMin->setValue(TaskManager::getInstance()->currentInstrument()->mdChinaNormalEWMin);
    ui->doubleSpinBox_NSMax->setValue(TaskManager::getInstance()->currentInstrument()->mdChinaNormalNSMax);
    ui->doubleSpinBox_NSMin->setValue(TaskManager::getInstance()->currentInstrument()->mdChinaNormalNSMin);
}

RegionScanForm::~RegionScanForm()
{
    delete ui;
}

void RegionScanForm::generateTask(TaskItem *pTask)
{
    if(!msCurTaskName.isEmpty() && NULL == pTask)// 更新任务
    {
        pTask = TaskManager::getInstance()->getTask(msCurTaskName);
    }

    CommandItem::FastTargetPos fastpos;
    for(int i=0; i<ui->tableWidget->rowCount(); ++i)
    {
        CommandItem* pCommandItem = pTask->getCommand(ui->tableWidget->item(i,0)->data(Qt::WhatsThisRole).toString());

        if(pCommandItem == NULL)
        {
            pCommandItem = pTask->addCommand(ui->tableWidget->item(i,0)->text());
            pCommandItem->setWorkMode(CommandItem::Mode_SlideScan);
        }
        fastpos.miTargetPosWorkMode="wait";

        fastpos.mdTargetPosDuration=ChinaScanstableTime;
        pCommandItem->setFastPosMode(fastpos);
        pCommandItem->setValue(ScriptItem::Pos_realEWstart,ui->tableWidget->item(i,0)->data(Qt::UserRole).toDouble());
        pCommandItem->setValue(ScriptItem::Pos_realEWend,ui->tableWidget->item(i,0)->data(Qt::UserRole+1).toDouble());
        pCommandItem->setValue(ScriptItem::Pos_EWStart,ui->tableWidget->item(i,Table_EWStart)->text().toDouble());
        pCommandItem->setValue(ScriptItem::Pos_EWEnd,ui->tableWidget->item(i,Table_EWEnd)->text().toDouble());
        pCommandItem->setValue(ScriptItem::Pos_NSStart,ui->tableWidget->item(i,Table_NSStart)->text().toDouble());
        pCommandItem->setValue(ScriptItem::Pos_NSEnd,ui->tableWidget->item(i,Table_NSEnd)->text().toDouble());
        pCommandItem->setValue(ScriptItem::Time_Start,ui->tableWidget->item(i,Table_STime)->text().toDouble());
        pCommandItem->setValue(ScriptItem::Time_End,ui->tableWidget->item(i,Table_ETime)->text().toDouble());
    }
}

void RegionScanForm::setCurrentTask(const QString &sName)
{
    msCurTaskName = sName;
    ui->lineEdit_CurTask->setText(sName);
    TaskEditor::setCurrentTask(sName);
}

void RegionScanForm::setCurrentMode(int nMode)
{
    // 不属于添加模式，部分按钮应该隐藏
    if(nMode != 0)
    {
        ui->doubleSpinBox_EWMax->setEnabled(false);
        ui->doubleSpinBox_EWMin->setEnabled(false);
        ui->doubleSpinBox_NSMax->setEnabled(false);
        ui->doubleSpinBox_NSMin->setEnabled(false);
    }

    TaskEditor::setCurrentMode(nMode);
}

// 编辑模式，更新当前任务
void RegionScanForm::updateTask(TaskItem *pTask)
{
    if(NULL == pTask)
        return;
    msCurTaskName = pTask->objectName();

    QVector<CommandItem *> vctCmds = pTask->allCommands();


    ui->tableWidget->setRowCount(vctCmds.count());
    for(int i=0; i<vctCmds.count(); ++i){
        QTableWidgetItem* pTableItem = new QTableWidgetItem(vctCmds.at(i)->getName());

        // 设置对象名称，为了在计算的时候不重新添加，同时可在更新的时候方便查找
        pTableItem->setData(Qt::WhatsThisRole,vctCmds.at(i)->objectName());

        ui->tableWidget->setItem(i,Table_Name,pTableItem);
        ui->tableWidget->setItem(i,Table_EWStart,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_EWStart),'f',6)));
        ui->tableWidget->setItem(i,Table_EWEnd,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_EWEnd),'f',6)));
        ui->tableWidget->setItem(i,Table_NSStart,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_NSStart),'f',6)));
        ui->tableWidget->setItem(i,Table_NSEnd,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_NSEnd),'f',6)));

        double dTimeStart = vctCmds.at(i)->getValue(ScriptItem::Time_Start);
        double dTimeEnd = vctCmds.at(i)->getValue(ScriptItem::Time_End);

        ui->tableWidget->setItem(i,Table_STime,new QTableWidgetItem(QString::number(dTimeStart,'f',6)));
        ui->tableWidget->setItem(i,Table_MidTime,new QTableWidgetItem(QString::number((dTimeStart+dTimeEnd)/2,'f',6)));
        ui->tableWidget->setItem(i,Table_ETime,new QTableWidgetItem(QString::number(dTimeEnd,'f',6)));
        ui->tableWidget->setItem(i,Table_TotTime,new QTableWidgetItem(QString::number(dTimeEnd-dTimeStart,'f',6)));
        if(i==0)
        {
            ui->tableWidget->setCurrentItem(pTableItem);
        }
    }
}

void RegionScanForm::on_pushButton_CalPos_clicked()
{
    // 按照矩形区域，单行扫描
    double dEWMin = ui->doubleSpinBox_EWMin->text().toDouble();
    double dEWMax = ui->doubleSpinBox_EWMax->text().toDouble();

    double dNSMin = ui->doubleSpinBox_NSMin->text().toDouble();
    double dNSMax = ui->doubleSpinBox_NSMax->text().toDouble();

    double dMirrHeight = TaskManager::getInstance()->currentInstrument()->mdMirrorHeight;
    //    double dMirrLeftHalf = TaskManager::getInstance()->currentInstrument()->mdMirrorLeftHalf;
    //    double dMirrRightHalf = TaskManager::getInstance()->currentInstrument()->mdMirrorRightHalf;

    //给出区域x大值和小值，能算出带加减匀速和稳定段和补偿的后的实际位置。
    double rlx[2]={0};

    //实际开始和结束位置
    double predistance=0.0;
    double afterdistance=0.0;

    TaskManager::getInstance()->currentInstrument()->getActuallocation(dEWMin,dEWMax,rlx,ui->doubleSpinBox_ScanSpeed->text().toDouble());
    predistance=dEWMin;
    afterdistance=dEWMax;
    double ewstart=rlx[0];
    double ewend=rlx[1];
    double nsstart=0.0;
    double nsend=0.0;

    //对齐的时间
    double dAlighTime = ui->doubleSpinBox_align->value();

    //扫描一行的时间
    double dTotTime=TaskManager::getInstance()->currentInstrument()->getScanTime(ewstart,ewend,ui->doubleSpinBox_ScanSpeed->text().toDouble());


#if 1
    int ScanRowNumber = ceil((dNSMax-dNSMin) / dMirrHeight);
    if(ui->comboBox_RegionMode->currentIndex() > 0)// 往返扫描模式
    {
        ScanRowNumber = ScanRowNumber%2 == 0? ScanRowNumber:ScanRowNumber+1;
    }

    for(int i=0; i<ScanRowNumber; ++i)
    {
        // 中间对齐
        if(ui->comboBox_PosAlign->currentIndex() == 0)
        {
            nsstart=(dNSMax-dNSMin)/2+dMirrHeight*((float)ScanRowNumber/2-(i+1))+dMirrHeight/2;
            nsend=nsstart;
        }
        //大数对齐，北侧，正向
        else if(ui->comboBox_PosAlign->currentIndex() == 1)
        {
            nsstart=dNSMax-dMirrHeight/2-dMirrHeight*i;
            nsend=nsstart;
        }
        // 小数对齐
        else
        {
            nsstart=dNSMin+dMirrHeight/2+(ScanRowNumber-(i+1))*dMirrHeight;
            nsend=nsstart;
        }
        if(mnWorkMode==0)
        {
            ui->tableWidget->setRowCount(ScanRowNumber);
            QTableWidgetItem *pNameItem=new QTableWidgetItem(tr("%1 %2/%3").arg(msCurTaskName).arg(i+1).arg(ScanRowNumber));
            pNameItem->setData(Qt::WhatsThisRole, QString("none"));
            ui->tableWidget->setItem(i,0,pNameItem);
            ui->tableWidget->item(i,0)->setData(Qt::UserRole,predistance);
            ui->tableWidget->item(i,0)->setData(Qt::UserRole+1,afterdistance);

            ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString::number(ewstart,'f',6)));
            ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString::number(ewend,'f',6)));
            ui->tableWidget->setItem(i,3,new QTableWidgetItem(QString::number(nsstart,'f',6)));
            ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(nsend,'f',6)));
        }

        //  中间时间对齐
        if(ui->comboBox_TimeAlign->currentIndex() == 0)
        {
            if(mnWorkMode==0)
            {
                ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(dAlighTime - dTotTime*0.5,'f',6)));
                ui->tableWidget->setItem(i,6,new QTableWidgetItem(QString::number(dAlighTime,'f',6)));
                ui->tableWidget->setItem(i,7,new QTableWidgetItem(QString::number(dAlighTime + dTotTime*0.5,'f',6)));
                ui->tableWidget->setItem(i,8,new QTableWidgetItem(QString::number(dTotTime,'f',6)));
            }
            else
            {
                ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
                ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime,'f',6));
                ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
                ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
            }
        }
        // 开始时间对齐
        else if(ui->comboBox_TimeAlign->currentIndex() == 1)
        {
            if(mnWorkMode==0)
            {
                ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(dAlighTime,'f',6)));
                ui->tableWidget->setItem(i,6,new QTableWidgetItem(QString::number(dAlighTime + dTotTime*0.5,'f',6)));
                ui->tableWidget->setItem(i,7,new QTableWidgetItem(QString::number(dAlighTime + dTotTime,'f',6)));
                ui->tableWidget->setItem(i,8,new QTableWidgetItem(QString::number(dTotTime,'f',6)));
            }
            else
            {
                ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime,'f',6));
                ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
                ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime,'f',6));
                ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
            }
        }
        // 结束时间对齐
        else
        {
            if(mnWorkMode==0)
            {
                ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(dAlighTime - dTotTime,'f',6)));
                ui->tableWidget->setItem(i,6,new QTableWidgetItem(QString::number(dAlighTime - dTotTime*0.5,'f',6)));
                ui->tableWidget->setItem(i,7,new QTableWidgetItem(QString::number(dAlighTime,'f',6)));
                ui->tableWidget->setItem(i,8,new QTableWidgetItem(QString::number(dTotTime,'f',6)));
            }
            else
            {
                ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime,'f',6));
                ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
                ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime,'f',6));
                ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
            }
        }

        dAlighTime += ui->doubleSpinBox_Cycle->value();

    }
#endif
    on_comboBox_TimeAlign_currentIndexChanged(ui->comboBox_TimeAlign->currentIndex());
}

void RegionScanForm::on_pushButton_AddCycle_clicked()
{
    for(int i=ui->tableWidget->currentRow(); i<ui->tableWidget->rowCount(); ++i)
    {
        double dTotTime =  ui->tableWidget->item(i,8)->text().toDouble();
        //  中间时间对齐
        if(ui->comboBox_TimeAlign->currentIndex() == 0)
        {
            double dAlighTime =ui->tableWidget->item(i,6)->text().toDouble() + ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 开始时间对齐
        else if(ui->comboBox_TimeAlign->currentIndex() == 1)
        {
            double dAlighTime =ui->tableWidget->item(i,5)->text().toDouble() + ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 结束时间对齐
        else
        {
            double dAlighTime =ui->tableWidget->item(i,7)->text().toDouble() + ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
    }
}

void RegionScanForm::on_pushButton_DeCycle_clicked()
{
    for(int i=ui->tableWidget->currentRow(); i>=0; --i)
    {
        double dTotTime =  ui->tableWidget->item(i,8)->text().toDouble();
        //  中间时间对齐
        if(ui->comboBox_TimeAlign->currentIndex() == 0)
        {
            double dAlighTime =ui->tableWidget->item(i,6)->text().toDouble() - ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 开始时间对齐
        else if(ui->comboBox_TimeAlign->currentIndex() == 1)
        {
            double dAlighTime =ui->tableWidget->item(i,5)->text().toDouble() - ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime + dTotTime,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 结束时间对齐
        else
        {
            double dAlighTime =ui->tableWidget->item(i,7)->text().toDouble() - ui->doubleSpinBox_Cycle->value();

            ui->tableWidget->item(i,5)->setText(QString::number(dAlighTime - dTotTime,'f',6));
            ui->tableWidget->item(i,6)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget->item(i,7)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
    }
}

void RegionScanForm::on_comboBox_TimeAlign_currentIndexChanged(int index)
{
    int AlignCol = -1;
    // 中间时间对齐
    if(index == 0)
    {
        AlignCol = 6;
    }
    // 开始时间对齐
    else if(index == 1)
    {
        AlignCol = 5;
    }
    // 结束时间对齐
    else
    {
        AlignCol = 7;
    }

    int iCurRow = ui->tableWidget->currentRow();
    if(iCurRow < 0)
        iCurRow = 0;

    ui->tableWidget->setCurrentCell(iCurRow,AlignCol);

    for(int i=0; i<ui->tableWidget->rowCount(); ++i)
    {
        for(int j=0; j<ui->tableWidget->columnCount(); ++j)
        {
            QTableWidgetItem* pItem = ui->tableWidget->item(i,j);
            if(pItem == NULL)
                continue;

            if(i == iCurRow && j == AlignCol)
            {
                pItem->setIcon(QIcon(QPixmap(":/image/res/redflag.png")));
            }
            else
            {
                pItem->setIcon(QIcon());
            }
        }
    }
}

void RegionScanForm::on_tableWidget_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
{
    int AlignCol = -1;
    // 中间时间对齐
    if(ui->comboBox_TimeAlign->currentIndex() == 0)
    {
        AlignCol = 6;
    }
    // 开始时间对齐
    else if(ui->comboBox_TimeAlign->currentIndex() == 1)
    {
        AlignCol = 5;
    }
    // 结束时间对齐
    else
    {
        AlignCol = 7;
    }

    if(ui->tableWidget->item(previousRow,AlignCol) != NULL)
        ui->tableWidget->item(previousRow,AlignCol)->setIcon(QIcon());

    if(NULL != ui->tableWidget->item(currentRow,AlignCol))
        ui->tableWidget->item(currentRow,AlignCol)->setIcon(QIcon(QPixmap(":/image/res/redflag.png")));
}
